package model

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

type PatientLesionsInfo struct {
	PatientStamp string
	OrganType    int
}

type LesionsInfo struct {
	LesionsId     int     `json:"tubercleId" gorm:"Column:tubercleId"`
	BoxId         int     `json:"bBoxId" gorm:"Column:bBoxId"`
	FbDiameter    float32 `json:"fbDiameter" gorm:"Column:fbDiameter"`
	LrDiameter    float32 `json:"lrDiameter" gorm:"Column:lrDiameter"`
	Area          float32 `json:"area" gorm:"Column:area"`
	Malignant     int     `json:"malignant" gorm:"Column:malignant"`
	MalignantProb float32 `json:"malignantProb" gorm:"Column:malignantProb"`
	TiradsValue   int     `json:"tiradsValue" gorm:"Column:tiradsValue"`
	ImageId       int     `json:"imageId" gorm:"Column:imageId"`
	ImageType     int     `json:"imageType" gorm:"Column:imageType"`
	X1            int     `json:"x1" gorm:"Column:x1"`
	Y1            int     `json:"y1" gorm:"Column:y1"`
	X2            int     `json:"x2" gorm:"Column:x2"`
	Y2            int     `json:"y2" gorm:"Column:y2"`
	IsVirtual     int     `json:"isVirtual" gorm:"Column:isVirtual"`
}

type ThyroidLesionsInfo struct {
	*LesionsInfo
	Lobe                     int `json:"lobe" gorm:"Column:lobe"`
	EchogenicFoci            int `json:"echogenicFoci" gorm:"Column:echogenicFoci"`
	MacroCalcifications      int `json:"macroCalcifications" gorm:"Column:macroCalcifications"`
	PeripheralCalcifications int `json:"peripheralCalcifications" gorm:"Column:peripheralCalcifications"`
	Comet                    int `json:"comet" gorm:"Column:comet"`
	Echogenic                int `json:"echogenic" gorm:"Column:echogenic"`
	Solid                    int `json:"solid" gorm:"Column:solid"`
	Circumscribed            int `json:"circumscribed" gorm:"Column:circumscribed"`
	Tallerthanwide           int `json:"tallerthanwide" gorm:"Column:tallerthanwide"`
}

type BreastLesionsInfo struct {
	*LesionsInfo
	MicroCalcifications int `json:"microCalcifications" gorm:"Column:microCalcifications"`
	EchoGenicity        int `json:"echoGenicity" gorm:"Column:echoGenicity"`
	Shape               int `json:"shape" gorm:"Column:shape"`
	Margin              int `json:"margin" gorm:"Column:margin"`
	Orientation         int `json:"orientation" gorm:"Column:orientation"`
	Acoustic            int `json:"acoustic" gorm:"Column:acoustic"`
	Glitch              int `json:"glitch" gorm:"Column:glitch"`
	Vague               int `json:"vague" gorm:"Column:vague"`
	Microlobulation     int `json:"microlobulation" gorm:"Column:microlobulation"`
	Angled              int `json:"angled" gorm:"Column:angled"`
	Hyperecho           int `json:"hyperecho" gorm:"Column:hyperecho"`
	TimeDirection       int `json:"timeDirection" gorm:"Column:timeDirection"`
	Pos                 int `json:"pos" gorm:"Column:pos"`
}

type PatientThyroidListSwagger struct {
	*SwaggerSuccess
	Data *ThyroidLesionsInfo `json:"data"`
}
type PatientBreastListSwagger struct {
	*SwaggerSuccess
	Data *BreastLesionsInfo `json:"data"`
}

// 查询甲状腺病灶列表
func (t PatientInfo) ThyroidLesionsList(db *gorm.DB) ([]*ThyroidLesionsInfo, error) {
	var lesions []*ThyroidLesionsInfo
	//存储过程接口
	query := fmt.Sprintf("call selectPatientTubercles(%s)", t.TimeStamp)
	//调用存储过程
	res := db.Raw(query).Scan(&lesions)
	if res.Error != nil {
		return nil, res.Error
	}

	return lesions, nil
}

// 查询乳腺病灶列表
func (t PatientInfo) BreastLesionsList(db *gorm.DB) ([]*BreastLesionsInfo, error) {
	var lesions []*BreastLesionsInfo
	//存储过程接口
	query := fmt.Sprintf("call selectPatientBreastTubercles(%s)", t.TimeStamp)
	//调用存储过程
	res := db.Raw(query).Scan(&lesions)
	if res.Error != nil {
		return nil, res.Error
	}

	return lesions, nil
}

// 根据图片信息查询甲状腺病灶信息
func (t PatientInfo) GetThyroidLesionsInfoByImageInfo(db *gorm.DB, imageId, imageType int) (*ThyroidLesionsInfo, error) {
	var lesions []*ThyroidLesionsInfo
	//拼接查询语句
	query := "select t2.id as tubercleId, t2.bBoxId, t2.fbDiameter,t2.lrDiameter,t2.area,t2.lobe, t2.malignant,"
	query += "t2.echogenicFoci,t2.macroCalcifications, t2.peripheralCalcifications,t2.comet,t2.echogenic,t2.solid,"
	query += "t2.circumscribed,t2.tallerthanwide, t2.tiradsValue, t1.id as imageId,t2.x1,t2.y1,t2.x2,t2.y2,"
	query += "t2.pos1,t2.pos2,t2.pos3,t2.soundHalo,t2.CDFI,t2.bloodSupply,t2.isVirtual,t2.malignantProb,t1.imageType "
	query += "from t_image_info t1 "
	query += fmt.Sprintf("left join t_tubercle_info t2 on t2.imageId=t1.id and t1.id = %d and t1.imageType = %d ", imageId, imageType)
	query += "where t1.isDelete=0 and t2.isDelete=0;"

	//执行查询语句
	res := db.Raw(query).Scan(&lesions)

	if res.Error != nil {
		return nil, res.Error
	}
	if len(lesions) > 0 {
		return lesions[0], nil
	}

	return nil, nil
}

// 根据图片信息查询乳腺病灶信息
func (t PatientInfo) GetBreastLesionsInfoByImageInfo(db *gorm.DB, imageId, imageType int) (*BreastLesionsInfo, error) {
	var lesions []*BreastLesionsInfo
	//拼接查询语句
	query := "select t2.id as tubercleId, t2.bBoxId, t2.fbDiameter,t2.lrDiameter,t2.area,t2.malignant,t2.microCalcifications,"
	query += "t2.echoGenicity,t2.shape,t2.margin,t2.orientation, t2.tiradsValue, t1.id as imageId,"
	query += "t2.x1,t2.y1,t2.x2,t2.y2,t2.CDFI,t2.acoustic,t2.isVirtual,t2.malignantProb,t2.timeDirection,t2.pos,t1.id as imageId, t1.imageType,"
	query += "t2.glitch,t2.vague,t2.microlobulation,t2.angled,t2.hyperecho "
	query += "from t_image_info t1 left join "
	query += fmt.Sprintf("t_breast_tubercle_info t2 on t2.imageId=t1.id and t1.id = %d and t1.imageType = %d where ", imageId, imageType)
	query += "t1.isDelete=0 and t2.isDelete=0;"

	//执行查询语句
	res := db.Raw(query).Scan(&lesions)

	if res.Error != nil {
		return nil, res.Error
	}
	if len(lesions) > 0 {
		return lesions[0], nil
	}

	return nil, nil
}

// 查询甲状腺病灶信息（按时间倒序）
func (t PatientInfo) GetThyroidLesionsListDescByTime(db *gorm.DB) ([]*ThyroidLesionsInfo, error) {
	var lesions []*ThyroidLesionsInfo
	query := getThyroidLesionsListDescByTimeSql("t_tubercle_info", t.TimeStamp)
	//调用查询语句
	res := db.Raw(query).Scan(&lesions)
	if res.Error != nil {
		return nil, res.Error
	}
	return lesions, nil
}

// 查询乳腺病灶信息（按时间倒序）
func (t PatientInfo) GetBreastLesionsListDescByTime(db *gorm.DB) ([]*BreastLesionsInfo, error) {
	var lesions []*BreastLesionsInfo
	query := getBreastLesionsListDescByTimeSql("t_breast_tubercle_info", t.TimeStamp)
	//调用查询语句
	res := db.Raw(query).Scan(&lesions)
	if res.Error != nil {
		return nil, res.Error
	}
	return lesions, nil
}

// 根据图片信息查询甲状腺病灶信息
func getThyroidLesionsListDescByTimeSql(tableName, patientStamp string) string {

	//拼接查询语句
	query := "select f.* from ("
	query += "select a2.tubercleId, a2.bBoxId, a2.fbDiameter,a2.lrDiameter, a2.area, a2.lobe,a2.malignant, a2.echogenicFoci,a2.macroCalcifications,a2.peripheralCalcifications,a2.comet,a2.echogenic,a2.solid,a2.circumscribed,a2.tallerthanwide, a2.tiradsValue, a2.imageId,a2.imageNTPStamp, a2.imageType,a2.x1,a2.y1,a2.x2,a2.y2,a2.pos1,a2.pos2,a2.pos3,a2.soundHalo,a2.CDFI,a2.bloodSupply,a2.isVirtual,a2.malignantProb "
	query += "from ("
	query += "select a1.tubercleId, a1.bBoxId, a1.fbDiameter,a1.lrDiameter, a1.area, a1.lobe,a1.malignant, a1.echogenicFoci,a1.macroCalcifications,a1.peripheralCalcifications,a1.comet,a1.echogenic,a1.solid,a1.circumscribed,a1.tallerthanwide, a1.tiradsValue, a1.imageId,a1.imageNTPStamp, a1.imageType,a1.x1,a1.y1,a1.x2,a1.y2,a1.pos1,a1.pos2,a1.pos3,a1.soundHalo,a1.CDFI,a1.bloodSupply,a1.isVirtual, a1.malignantProb,"
	query += "if(@pdept=a1.bBoxId, @rank:=@rank+1, @rank:=1) as r, @pdept:=a1.bBoxId "
	query += "from ("
	query += "select t3.id as tubercleId, t3.bBoxId, t3.fbDiameter,t3.lrDiameter,t3.area, t3.lobe,t3.malignant,t3.echogenicFoci,t3.macroCalcifications,t3.peripheralCalcifications,t3.comet,t3.echogenic,t3.solid,t3.circumscribed,t3.tallerthanwide, t3.tiradsValue, t2.id as imageId,t2.imageNTPStamp as imageNTPStamp,t2.imageType,t3.x1,t3.y1,t3.x2,t3.y2,t3.pos1,t3.pos2,t3.pos3,t3.soundHalo,t3.CDFI,t3.bloodSupply,t3.isVirtual,t3.malignantProb "
	query += "from t_patient_info t1 "
	query += "left join t_image_info t2 on t1.id = t2.patientId and t2.imageType = 0 "
	query += fmt.Sprintf("left join %s t3 on t1.id = t3.patientId and t2.id = t3.imageId ", tableName)
	query += fmt.Sprintf("where t1.timeStamp = %s and t1.isDelete = 0 and t2.isDelete = 0 and t3.isDelete = 0 ", patientStamp)
	query += "order by t3.bBoxId asc, t3.tiradsValue desc, t3.area desc, t2.imageNTPStamp asc) a1, "
	query += "(select @pdept := null ,@rank:=0) b ) as a2 where r <= 1"

	query += " union all "

	query += "select b.tubercleId, b.bBoxId, b.fbDiameter, b.lrDiameter, b.area, b.lobe, b.malignant, b.echogenicFoci,b.macroCalcifications,b.peripheralCalcifications,b.comet,b.echogenic, b.solid, b.circumscribed, b.tallerthanwide, b.tiradsValue, b.imageId, b.imageNTPStamp,b.imageType,b.x1, b.y1, b.x2, b.y2,b.pos1,b.pos2,b.pos3,b.soundHalo,b.CDFI,b.bloodSupply,b.isVirtual,b.malignantProb "
	query += "from ("
	query += "select t3.id as tubercleId, t3.bBoxId, t3.fbDiameter,t3.lrDiameter,t3.area, t3.lobe,t3.malignant,t3.echogenicFoci,t3.macroCalcifications,t3.peripheralCalcifications,t3.comet,t3.echogenic,t3.solid,t3.circumscribed,t3.tallerthanwide, t3.tiradsValue, t2.id as imageId,t2.imageNTPStamp as imageNTPStamp,t2.imageType,t3.x1,t3.y1,t3.x2,t3.y2,t3.pos1,t3.pos2,t3.pos3,t3.soundHalo,t3.CDFI,t3.bloodSupply,t3.isVirtual,t3.malignantProb "
	query += "from t_patient_info t1 "
	query += "left join t_image_info t2 on t1.id = t2.patientId and t2.imageType = 1 "
	query += fmt.Sprintf("left join %s t3 on t3.imageId = t2.id and t3.patientId = t1.id ", tableName)
	query += fmt.Sprintf("where t1.timeStamp = %s and t1.isDelete=0 and t2.isDelete=0 and t3.isDelete=0) as b ", patientStamp)
	query += "order by tiradsValue desc, area desc, imageNTPStamp asc"
	query += ") as f order by imageNTPStamp desc;"

	return query
}

// 根据图片信息查询乳腺病灶信息
func getBreastLesionsListDescByTimeSql(tableName, patientStamp string) string {
	query := "select f.* from ("
	query += "select a2.tubercleId, a2.bBoxId, a2.fbDiameter,a2.lrDiameter, a2.area,a2.malignant, a2.microCalcifications,a2.echoGenicity,a2.shape,a2.margin,a2.orientation, a2.tiradsValue, a2.imageId,a2.imageNTPStamp, a2.imageType,a2.x1,a2.y1,a2.x2,a2.y2,a2.CDFI,a2.acoustic,a2.isVirtual,a2.malignantProb,a2.timeDirection,a2.pos,a2.glitch,a2.vague,a2.microlobulation,a2.angled,a2.hyperecho "
	query += "from ("
	query += "select a1.tubercleId, a1.bBoxId, a1.fbDiameter,a1.lrDiameter, a1.area,a1.malignant, a1.microCalcifications,a1.echoGenicity,a1.shape,a1.margin,a1.orientation, a1.tiradsValue, a1.imageId,a1.imageNTPStamp, a1.imageType,a1.x1,a1.y1,a1.x2,a1.y2,a1.CDFI,a1.acoustic,a1.isVirtual, a1.malignantProb,a1.timeDirection,a1.pos,a1.glitch,a1.vague,a1.microlobulation,a1.angled,a1.hyperecho,"
	query += "if(@pdept=a1.bBoxId, @rank:=@rank+1, @rank:=1) as r, @pdept := a1.bBoxId "
	query += "from ("
	query += "select t3.id as tubercleId, t3.bBoxId, t3.fbDiameter,t3.lrDiameter,t3.area,t3.malignant,t3.microCalcifications,t3.echoGenicity,t3.shape,t3.margin,t3.orientation, t3.tiradsValue, t2.id as imageId,t2.imageNTPStamp as imageNTPStamp,t2.imageType,t3.x1,t3.y1,t3.x2,t3.y2,t3.CDFI,t3.acoustic,t3.isVirtual,t3.malignantProb,t3.timeDirection,t3.pos,t3.glitch,t3.vague,t3.microlobulation,t3.angled,t3.hyperecho "
	query += "from t_patient_info t1 "
	query += "left join t_image_info t2 on t1.id = t2.patientId and t2.imageType = 0 "
	query += fmt.Sprintf("left join %s t3 on t1.id = t3.patientId and t2.id = t3.imageId ", tableName)
	query += fmt.Sprintf("where t1.timeStamp = %s and t1.isDelete = 0 and t2.isDelete = 0 and t3.isDelete = 0 ", patientStamp)
	query += "order by t3.bBoxId asc, t3.tiradsValue desc, t3.area desc, t2.imageNTPStamp asc "
	query += ") as a1,"
	query += "(select @pdept := null ,@rank:=0) b"
	query += ") as a2 where r <= 1"

	query += " union all "

	query += "select b.tubercleId, b.bBoxId, b.fbDiameter, b.lrDiameter, b.area, b.malignant, b.microCalcifications, b.echoGenicity, b.shape, b.margin, b.orientation, b.tiradsValue, b.imageId, b.imageNTPStamp,b.imageType,b.x1, b.y1, b.x2, b.y2,b.CDFI,b.acoustic,b.isVirtual,b.malignantProb,b.timeDirection,b.pos,b.glitch,b.vague,b.microlobulation,b.angled,b.hyperecho "
	query += "from ("
	query += "select t3.id as tubercleId, t3.bBoxId, t3.fbDiameter,t3.lrDiameter,t3.area, t3.malignant,t3.microCalcifications,t3.echoGenicity,t3.shape,t3.margin,t3.orientation, t3.tiradsValue, t2.id as imageId,t2.imageNTPStamp as imageNTPStamp,t2.imageType,t3.x1,t3.y1,t3.x2,t3.y2,t3.CDFI,t3.acoustic,t3.isVirtual,t3.malignantProb,t3.timeDirection,t3.pos,t3.glitch,t3.vague,t3.microlobulation,t3.angled,t3.hyperecho "
	query += "from t_patient_info t1 "
	query += "left join t_image_info t2 on t1.id = t2.patientId and t2.imageType = 1 "
	query += fmt.Sprintf("left join %s t3 on t3.imageId = t2.id and t3.patientId = t1.id ", tableName)
	query += fmt.Sprintf("where t1.isDelete=0 and t2.isDelete=0 and t3.isDelete=0 and t1.timeStamp= %s) as b ", patientStamp)
	query += "order by tiradsValue desc, area desc, imageNTPStamp asc"
	query += ") as f order by imageNTPStamp desc;"
	return query
}
